package com.anolesoft.yr.mbus.crm.controller;

import com.anolesoft.common.core.web.domain.AjaxResult;
import com.anolesoft.yr.mbus.crm.dto.CrmBaseDTO;
import com.anolesoft.yr.mbus.crm.service.CrmBaseService;
import com.anolesoft.yr.mbus.crm.vo.CrmBaseVO;
import com.anolesoft.yr.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author zhj
 * @date 2020/8/9 19:02
 */
@RestController
public class CrmBaseController {
    @Autowired
    private CrmBaseService crmBaseService;
//    private CrmBaseVO dto2vo(CrmBaseDTO t) {
//        BaseRegionDTO region = t.getRegion();
//        BaseRegionVO baseRegionVO = null;
//        if (region != null) {
//            baseRegionVO = BaseRegionVO.builder()
//                    .remark(region.getRemark())
//                    .parentUid(StringUtils.isEmpty(region.getParentUid()) ? "-" : region.getParentUid())
//                    .modifyUserUid(region.getModifyUserUid())
//                    .deleteFlag(region.getDeleteFlag())
//                    .createUserUid(region.getCreateUserUid())
//                    .regionCode(region.getRegionCode())
//                    .regionLevel(region.getRegionLevel())
//                    .regionName(region.getRegionName())
//                    .factoryCode(region.getFactoryCode())
//                    .objuid(region.getObjuid())
//                    .build();
//        } else {
//            baseRegionVO = new BaseRegionVO();
//        }
//        List<BaseContactVO> contactVOS = t.getContacts().stream().map(baseContactDTO -> BaseContactVO.builder()
//                .contactNumber(baseContactDTO.getContactNumber())
//                .contactPerson(baseContactDTO.getContactPerson())
//                .createTime(baseContactDTO.getCreateTime())
//                .createUserUid(baseContactDTO.getCreateUserUid())
//                .crmAddress(baseContactDTO.getCrmAddress())
//                .deleteFlag(baseContactDTO.getDeleteFlag())
//                .factoryCode(baseContactDTO.getFactoryCode())
//                .modifyTime(baseContactDTO.getModifyTime())
//                .modifyUserUid(baseContactDTO.getModifyUserUid())
//                .objuid(baseContactDTO.getObjuid())
//                .postcode(baseContactDTO.getPostcode())
//                .remark(baseContactDTO.getRemark())
//                .targetUid(baseContactDTO.getTargetUid())
//                .type(baseContactDTO.getType())
//                .build()).collect(Collectors.toList());
//        return CrmBaseVO.builder()
//                .account(t.getAccount())
//                .bank(t.getBank())
//                .baseRegionUid(t.getBaseRegionUid())
//                .crmAddress(t.getCrmAddress())
//                .crmCode(t.getCrmCode())
//                .crmGrade(t.getCrmGrade())
//                .crmName(t.getCrmName())
//                .crmOtherName(t.getCrmOtherName())
//                .companyType(t.getCompanyType())
//                .crmWebsite(t.getCrmWebsite())
//                .contactNumber(t.getContactNumber())
//                .contactPerson(t.getContactPerson())
//                .createTime(t.getCreateTime())
//                .createUserUid(t.getCreateUserUid())
//                .deleteFlag(t.getDeleteFlag())
//                .email(t.getEmail())
//                .establishTime(t.getEstablishTime())
//                .factoryCode(t.getFactoryCode())
//                .faxNumber(t.getFaxNumber())
//                .legalPerson(t.getLegalPerson())
//                .modifyTime(t.getModifyTime())
//                .modifyUserUid(t.getModifyUserUid())
//                .objuid(t.getObjuid())
//                .region(baseRegionVO)
//                .remark(t.getRemark())
//                .taxNumber(t.getTaxNumber())
//                .contacts(contactVOS)
//                .build();
//    }

    @PostMapping(value = "/api/crm/crmbase/add", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult create(CrmBaseVO crmBaseVO) {
        CrmBaseDTO crmBaseDTO = vo2dto(crmBaseVO, true);
        System.out.println();
        return new AjaxResult(HttpStatus.SUCCESS, Constants.CREATE_SUCCESS_MESSAGE, crmBaseService.create(crmBaseDTO));
    }

    private CrmBaseDTO vo2dto(CrmBaseVO crmBaseVO,boolean newId) {
        return CrmBaseDTO.builder()
                    .crmAddress(crmBaseVO.getCrmAddress())
                    .crmOtherName(crmBaseVO.getCrmOtherName())
                    .crmName(crmBaseVO.getCrmName())
                    .crmCode(crmBaseVO.getCrmCode())
                    .baseRegionUid(crmBaseVO.getBaseRegionUid())
                    .factoryCode(crmBaseVO.getFactoryCode())
                    .modifyUserUid(SecurityUtils.getUserId())
                    .taxNumber(crmBaseVO.getTaxNumber())
                    .remark(crmBaseVO.getRemark())
                    .objuid(newId ? CommonUtils.fastUUID() : crmBaseVO.getObjuid())
                    .legalPerson(crmBaseVO.getLegalPerson())
                    .faxNumber(crmBaseVO.getFaxNumber())
                    .establishTime(crmBaseVO.getEstablishTime())
                    .email(crmBaseVO.getEmail())
                    .createUserUid(SecurityUtils.getUserId())
                    .contactPerson(crmBaseVO.getContactPerson())
                    .contactNumber(crmBaseVO.getContactNumber())
                    .crmWebsite(crmBaseVO.getCrmWebsite())
                    .companyType(crmBaseVO.getCompanyType())
                    .crmGrade(crmBaseVO.getCrmGrade())
                    .bank(crmBaseVO.getBank())
                    .account(crmBaseVO.getAccount())
                    .build();
    }

    @PutMapping(value = "/api/crm/crmbase/update", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult modify(CrmBaseVO crmBaseVO) {
        CrmBaseDTO crmBaseDTO = vo2dto(crmBaseVO, false);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.MODIFY_SUCCESS_MESSAGE, crmBaseService.modify(crmBaseDTO));
    }

    @DeleteMapping(value = "/api/crm/crmbase/delete/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult remove(@PathVariable("id") String id) {
        return new AjaxResult(HttpStatus.SUCCESS, Constants.DELETE_SUCCESS_MESSAGE, crmBaseService.remove(id));
    }

    @DeleteMapping(value = "/api/crm/crmbase/delete", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult removeBatch(@RequestParam("ids[]") List<String> ids) {
        return new AjaxResult(HttpStatus.SUCCESS, Constants.DELETE_SUCCESS_MESSAGE, crmBaseService.removeBatch(ids));
    }

    @GetMapping(value = "/api/crm/crmbase/get/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectOneById(@PathVariable("id") String id) {
        Optional<CrmBaseDTO> crmBaseDTO = this.crmBaseService.selectOneById(id);
        if (crmBaseDTO.isPresent()) {
            return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, CustomBeanUtils.crmBaseDTO2VO(crmBaseDTO.get()));
        } else {
            return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, null);
        }

    }

    @GetMapping(value = "/api/crm/crmbase/list/all", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectAll() {
        List<CrmBaseVO> vos = crmBaseService.selectAll().stream().map(t -> CustomBeanUtils.crmBaseDTO2VO(t)).collect(Collectors.toList());
        PageInfo<List<CrmBaseVO>> pageInfo = new PageInfo<>(vos.size(), vos);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, pageInfo);
    }

    @GetMapping(value = "/api/crm/crmbase/list/all/page", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectPageAll(
            @RequestParam("rows") Integer limit,
            @RequestParam("page") Integer curr) {
//        List<CrmBaseVO> vos = crmBaseService.selectAll(limit, curr).stream().map(t -> dto2vo(t)).collect(Collectors.toList());
//        PageInfo<List<CrmBaseVO>> pageInfo = new PageInfo<>(limit, vos.size(), vos);
        PageData<List<CrmBaseDTO>> pageData = crmBaseService.selectAll(limit, curr);
        List<CrmBaseVO> vos = pageData.getItems().stream().map(t -> CustomBeanUtils.crmBaseDTO2VO(t)).collect(Collectors.toList());
        PageInfo<List<CrmBaseVO>> pageInfo = new PageInfo<>(limit, pageData.getTotal(), vos);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, pageInfo);
    }

    @GetMapping(value = "/api/crm/crmbase/list/condition", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectList(CrmBaseVO crmBaseVO) {
        CrmBaseDTO crmBaseDTO = vo2dto(crmBaseVO, false);
        List<CrmBaseVO> vos = this.crmBaseService.selectList(crmBaseDTO).stream().map(t -> CustomBeanUtils.crmBaseDTO2VO(t)).collect(Collectors.toList());
        PageInfo<List<CrmBaseVO>> pageInfo = new PageInfo<>(vos.size(), vos);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, pageInfo);
    }

    @GetMapping(value = "/api/crm/crmbase/list/condition/page", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectListPage(CrmBaseVO crmBaseVO) {
        CrmBaseDTO crmBaseDTO = vo2dto(crmBaseVO, false);
        Integer limit = crmBaseVO.getRows();
        Integer curr = crmBaseVO.getPage();
//        List<CrmBaseVO> vos = this.crmBaseService.selectList(crmBaseDTO, limit, curr).stream().map(t -> dto2vo(t)).collect(Collectors.toList());
//        PageInfo<List<CrmBaseVO>> pageInfo = new PageInfo<>(limit, vos.size(), vos);
        PageData<List<CrmBaseDTO>> pageData = crmBaseService.selectList(crmBaseDTO, limit, curr);
        List<CrmBaseVO> vos = pageData.getItems().stream().map(t -> CustomBeanUtils.crmBaseDTO2VO(t)).collect(Collectors.toList());
        PageInfo<List<CrmBaseVO>> pageInfo = new PageInfo<>(limit, pageData.getTotal(), vos);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, pageInfo);
    }

    @PostMapping(value = "/api/crm/crmbase/list/combox", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectComboxList(@RequestBody CrmBaseVO crmBaseVO) {
        CrmBaseDTO crmBaseDTO = vo2dto(crmBaseVO, false);
        Integer limit = crmBaseVO.getRows();
        Integer curr = crmBaseVO.getPage();
        PageData<List<CrmBaseDTO>> pageData = crmBaseService.selectComboxList(crmBaseDTO, limit, curr);
        List<CrmBaseVO> vos = pageData.getItems().stream().map(CustomBeanUtils::crmBaseDTO2VO).collect(Collectors.toList());
        PageInfo<List<CrmBaseVO>> pageInfo = new PageInfo<>(limit, pageData.getTotal(), vos);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, pageInfo);
    }
}
